package com.dubbo.web.util;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;

/**   
* @Title: JacksonUtils.java 
* @Package com.dubbo.web.util 
* @Description: TODO(用一句话描述该文件做什么) 
* @author liusongqing 
* @Company www.viewhigh.com
* @date 2016年8月30日 下午4:39:43 
* @version V1.0   
*/
public class JacksonUtils {
    private static final ObjectMapper mapper = new ObjectMapper();

    private JacksonUtils() {
    }

    static {
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
        // mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT);
        mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        // 不区分下划线 但是 属性集中不能存在驼峰式
        // mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
    }

    public static <T> T readValue(String jsonStr, Class<T> clazz) {
        try {
            T t = mapper.readValue(jsonStr, clazz);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json 字符串 转换成 List对象
     * 
     * @param <T>
     * @param json
     * @param tClazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> readListValue(String json, Class<T> tClazz) {
        return readValue(json, java.util.List.class, tClazz);
    }

    /**
     * Json 字符串 转换成Collection对象
     * 
     * @param <U>
     * @param <T>
     * @param json
     * @param uClazz
     * @param tClazz
     * @return
     */
    public static <U extends Collection<T>, T> U readValue(String json, Class<U> uClazz, Class<T> tClazz) {
        try {
            return mapper.readValue(json, TypeFactory.defaultInstance().constructCollectionType(uClazz, tClazz));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * json 转换成Map
     * 
     * @param <T>
     * @param json
     * @param tClazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> HashMap<String, T> readMapValue(String json, Class<T> tClazz) {
        return readMapValue(json, java.util.HashMap.class, tClazz);
    }

    /**
     * json 转换成Map
     * 
     * @param <U>
     * @param <T>
     * @param json
     * @param uClazz
     * @param tClazz
     * @return
     */
    public static <U extends Map<String, T>, T> U readMapValue(String json, Class<U> uClazz, Class<T> tClazz) {
        try {
            return mapper.readValue(json,
                    TypeFactory.defaultInstance().constructMapType(uClazz, java.lang.String.class, tClazz));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String toJson(Object t) {

        String result = null;
        if (t == null) {
            return result;
        }
        try {
            result = mapper.writeValueAsString(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}

